<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="sharpicons_Horse" />
            <span> 江苏传智播客教育科技股份有限公司 </span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <hr>
        <!-- 树形结构 -->
        <el-tree
          :data="list"
          node-key="id"
          default-expand-all
          :expand-on-click-node="false"
        >
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span> {{ data.name }} </span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addHandel(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item>编辑子部门</el-dropdown-item>
                        <el-dropdown-item>删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- 弹窗 -->
        <el-dialog
          title="添加部门"
          :visible.sync="showDialog"
          width="width"
          @close="close"
        >
          <!-- 这里放表单内容 -->
          <dept-dialog
            ref="deptDg"
            :pid="pid"
            :origin-list="originList"
            @success="hSuccess"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getDepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      pid: '', // 部门id
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      originList: []
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    close() {
      this.$refs.deptDg.close()
    },
    hSuccess() {
      this.getDepartment()
      this.showDialog = false
    },
    // 添加部门
    addHandel(id) {
      this.pid = id
      this.showDialog = true
    },
    async getDepartment() {
      const res = await getDepartment()
      res.data.depts.splice(0, 1)
      this.originList = res.data.depts.map((item) => {
        return { id: item.id, name: item.name, code: item.code, pid: item.pid }
      })
      this.list = tranListToTreeData(res.data.depts)
    },
    handleNodeClick(data) {
      console.log(data)
    }
  }
}
</script>
